package com.sntk.organauthomanager.domain.model;

import com.sntk.organauthomanager.infrastructure.exception.DataNoLegalException;
import com.sntk.organauthomanager.infrastructure.exception.ObjectDisbaledException;
import lombok.Getter;
import org.springframework.util.StringUtils;

/**
 * @ClassName: Role
 * @author: wgchao
 * @createTime: 2020/7/17 3:05 PM
 * @Description: 角色
 */
@Getter
public class Role extends AggregateRoot {
    //唯一标识
    private RoleIdentify roleIdentify;

    //角色名
    private String roleName;

    //备注
    private String remark;

    private boolean disabled;


    public Role(Long id, RoleIdentify roleIdentify, String roleName, String remark, boolean disabled, Long version) {
        if(StringUtils.isEmpty(roleName)){
            throw new DataNoLegalException("角色名不能为空");
        }
        setId(id);
        this.roleIdentify = roleIdentify;
        this.roleName = roleName;
        this.remark = remark;
        this.disabled = disabled;
        setVersion(version);
    }

    public static Role generate(Organization organization, String roleCode, String roleName, String remark){
        if(organization == null){
            throw new DataNoLegalException("角色所属机构信息不能为空");
        }
        organization.normalException();
        return new Role(-1L, new RoleIdentify(organization.getInstitutionNumber(), roleCode), roleName,
                remark, false, null);
    }

    public void modifyRoleInfo(String roleName, String remark, boolean disabled){
        if(StringUtils.isEmpty(roleName)){
            throw new DataNoLegalException("角色名不能为空");
        }
        this.roleName = roleName;
        this.remark = remark;
        this.disabled = disabled;
    }

    public boolean normal(){
        return !this.disabled;
    }

    public void normalException(){
        if(!normal()){
            throw new ObjectDisbaledException(this.roleName+"角色不可用");
        }
    }

}
